package TUILayer;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class InputParser {
	public InputParser()
    {
    }

	public int getInt(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        int value = 0;
        while(!exit)
        {
            System.out.print(text);
            String input = keyboard.nextLine();
            if(!input.equals(""))
            {
                try
                {
                    value = Integer.parseInt(input);  
                    exit = true;
                }
                catch(NumberFormatException nFE) {

                }
            }
        }
        return value;
    }
	
	public double getDouble(String text)
    {
		Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        double value = 0;
        while(!exit)
        {
            System.out.print(text);
            String input = keyboard.nextLine();
            if(!input.equals(""))
            {
                try
                {
                    value = Double.parseDouble(input);
                    exit = true;
                }
                catch(NumberFormatException nFE) {

                }
            }
        }
        return value;
    }
    
    public long getLong(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        long value = 0;
        while(!exit)
        {
            System.out.print(text);
            String input = keyboard.nextLine();
            if(!input.equals(""))
            {
                try
                {
                    value = Long.parseLong(input);  
                    exit = true;
                }
                catch(NumberFormatException nFE) {

                }
            }
        }
        return value;
    }

    public String getString(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        String value = "";
        while(!exit)
        {
            System.out.print(text);
            value = keyboard.nextLine();
            if(!value.equals(""))
            {
                exit = true;
            }
        }
        return value;
    }

    public long getDate(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        boolean ok = true;
        String input = "";
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        formatter.setLenient(false);
        java.util.Date utilDate = null;
        while(!exit)
        {
            System.out.print(text);
            input = keyboard.nextLine();
            if(!input.equals(""))
            {
                try {
                    utilDate = formatter.parse(input);
                    exit = true;
                    ok = true;
                } catch (ParseException e) {
                    ok = false;
                }
                if(ok == true)
                {
                    if(!formatter.format(utilDate).equals(input)) 
                    {
                        exit = false;
                    }
                }
            }
        }
        return utilDate.getTime();
    }

    public void printReturn()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\nPress Enter to return...");
        keyboard.nextLine();
    }
    
    public long getCurrentDate()
    {
        Calendar cal = Calendar.getInstance();
        java.util.Date utilDate = cal.getTime();
        return utilDate.getTime();
    }
    
    public int[] getMonthYear(String text)
    {
    	Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        int[] result = { 0, 0 };
        while(!exit)
        {
            System.out.print(text);
            String input = keyboard.nextLine();
            if(input.matches("(0[1-9]|1[0-2])/[1-9][0-9][0-9][0-9]"))
            {
                try
                {
                    result[0] = Integer.parseInt(input.substring(0,2));  
                    result[1] = Integer.parseInt(input.substring(3,7));
                    exit = true;
                }
                catch(NumberFormatException nFE) {

                }
            }
        }
    	return result;
    }
    
    public double getPercent(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        double value = 0.0;
        while(!exit)
        {
            System.out.print(text);
            String input = keyboard.nextLine();
            if(!input.equals(""))
            {
                try
                {
                    double testvalue = Double.parseDouble(input);
                    if(testvalue > 0 && testvalue < 100)
                    {    
                    	value = testvalue / 100.0;
                    	exit = true;
                    }
                }
                catch(NumberFormatException nFE) {

                }
            }
        }
        return value;
    }

}
